<?php
/*
	ТО DO при регистрации проверять существование такого логина 
	Сделать функцию проверки данных в фронте
	создать файл конфиг для хранения данных о БД
	проверить данные на инъекции и на пустоту
*/
class FrontController
{ 
	private $params; 
	private static $frontController; 
	private $view; 
	
	private function __construct() {
		$this->params = array('get' => $_GET, 'post' => $_POST); 
		$this->view = new View(); 
		if (isset($_SESSION['user'])) {
            $user = unserialize($_SESSION['user']);
			$this->view->setUser($user);
		}
	} 
	public static function instance() { 
		if(null == FrontController::$frontController) { 
			FrontController::$frontController = new FrontController(); 
		} 
		return FrontController::$frontController; 
	} 
	
	public function run() { 
		if(empty($this->params['get']['controller'])) { 
			$actionController = ActionControllerFabric::getController("Index", $this->view, $this->params); 
		} else { 
			switch ($this->params['get']['controller']) { 
				case 'article': 
					$actionController = ActionControllerFabric::getController("Article", $this->view, $this->params); 
					break; 
				case 'karma': 
					$actionController = ActionControllerFabric::getController("Karma", $this->view, $this->params); 
					break; 
				case 'admin': 
					$actionController = ActionControllerFabric::getController("Admin", $this->view, $this->params); 
					break; 
				case 'registration': 
					$actionController = ActionControllerFabric::getController("Registration", $this->view, $this->params); 
					break; 
				case 'edit': 
					$actionController = ActionControllerFabric::getController("Edit", $this->view, $this->params); 
					break; 
				case 'enter': 
					$actionController = ActionControllerFabric::getController("Enter", $this->view, $this->params); 
					break; 
				default: 
					$actionController = ActionControllerFabric::getController("Index", $this->view); 
			} 
		} 
		$actionController->run(); 	
		$this->view->render(); 
	}
}
